var obj;
var thishome;
var HomePageMediator = Objs("view.HomePageMediator",
	Mediator,
{
	initialize: function( name, viewComponent )
	{  
		HomePageMediator.$super.initialize.call( this, name, viewComponent );
		this.VProxy = this.facade.retrieveProxy( ProxyNames.VEHICLE_PROXY );
		this.registerListeners();
               this.initializePage();
		thishome = this;
		
	},
        initializePage: function()
        {
             if(current_page==AppConstants.VEHICLE_LIST)
           {    
               if(global_user.admin_type=="Tower Administrator")
               { 
                    var tab;
                  //var tab = $.urlParam('tab');
                  tab='4';
                     switch(tab)
                    {
                        case '1':
                                  this.newTowVehicleHandler();
                                  break;
                        case '2':
                                   
                                  this.inprogressTowVehicleHandler();
                                  break;
                        case '3':    
                                 this.deliveredTowVehicleHandler();
                                 break;

                        case '4':
                                 this.allTowVehicleHandler();
                                 break;
                        default :
                               //  this.newTowVehicleHandler();   
                                this.allTowVehicleHandler();
                    }
                  
               }
               else
               {
                  
                  
                    switch(getUrlVars()["tab"])
                    {
                    case '1':   
                            
                                 if(global_user.tab_newvehicle!=0)
                                   {  
                                   
                                    this.newSubmissionHandler();
                                   } 
                                 else
                                     {    
                                         if(global_user.tab_bidaccepted!=0)
                                             this.bisAcceptedHandler();
                                         else if( global_user.tab_inpossession!=0)
                                             this.inPossessionHandler();
                                         else if(global_user.tab_allvehicles!=0)
                                               this.allVehicleHandler();    
                                     }
                                 break;
                    case '2':
                                if(global_user.tab_bidaccepted!=0)
                                 this.bisAcceptedHandler();
                                else
                                    {
                                        if(global_user.tab_newvehicle!=0)
                                            this.newSubmissionHandler();
                                        else if( global_user.tab_inpossession!=0)
                                             this.inPossessionHandler();
                                         else if(global_user.tab_allvehicles!=0)
                                               this.allVehicleHandler();   
                                    }
                                  break;

                    case '3':    
                                if( global_user.tab_inpossession!=0)
                                 this.inPossessionHandler();
                                 else
                                     {
                                         if(global_user.tab_newvehicle!=0)
                                            this.newSubmissionHandler();
                                         else if(global_user.tab_bidaccepted!=0)
                                             this.bisAcceptedHandler();
                                         else if(global_user.tab_allvehicles!=0)
                                               this.allVehicleHandler();   
                                     }
                                 break;

                    case '4':    
                                 if(global_user.tab_allvehicles!=0)
                                 this.allVehicleHandler();
                                 break;
                                 {
                                     if(global_user.tab_newvehicle!=0)
                                            this.newSubmissionHandler();
                                         else if(global_user.tab_bidaccepted!=0)
                                             this.bisAcceptedHandler();
                                         else if( global_user.tab_inpossession!=0)
                                             this.inPossessionHandler();
                                        
                                 }
                    default :   
                                if(global_user.tab_newvehicle!=0)
                                    this.newSubmissionHandler();
                                 else
                                     {
                                         if(global_user.tab_bidaccepted!=0)
                                             this.bisAcceptedHandler();
                                         else if( global_user.tab_inpossession!=0)
                                             this.inPossessionHandler();
                                         else if(global_user.tab_allvehicles!=0)
                                               this.allVehicleHandler();    
                                     }

                    }
                   
               }    
           
           }   
            
            
          if(global_user.admin_type=="Tower Administrator")
          { 
                $("#vehicle-list-page").show();
                $("#vehicle-list-area").show();
                $("#vehicle-list-component").show();
                $("#page-map").hide();
                $("#vehicle-list_button").button('toggle');
                
              /*   $("#c").val('all');
                 
                  $('li').removeClass('active').addClass('inactive');
		  $('#ta-new-vehicle-component').removeClass('inactive').addClass('active');  
		//document.getElementById("s").value = '';
		currentStatus = 1;
		this.VProxy.getVehicleByPage(1);  */
          }  
        },
        
	registerListeners: function()
	{
		var homePage = this.get();	
              
		homePage.addEventListener( HomePage.ADD_NEW_VEHICLE, this.addNewVehicleHandler, this );
		homePage.addEventListener( HomePage.STATUS_NEW_SUBMISSION, this.newSubmissionHandler, this );
		homePage.addEventListener( HomePage.STATUS_BID_ACCEPTED, this.bisAcceptedHandler, this );
		homePage.addEventListener( HomePage.STATUS_IN_POSSEESSION, this.inPossessionHandler, this );
		homePage.addEventListener( HomePage.STATUS_ALL_VEHICLE, this.allVehicleHandler, this );
                
                homePage.addEventListener( HomePage.STATUS_TOW_NEW_VEHICLE, this.newTowVehicleHandler, this );
                
                homePage.addEventListener( HomePage.STATUS_TOW_INPROGRESS_VEHICLE, this.inprogressTowVehicleHandler, this );
                homePage.addEventListener( HomePage.STATUS_TOW_DELIVERED_VEHICLE, this.deliveredTowVehicleHandler, this );
                homePage.addEventListener( HomePage.STATUS_TOW_ALL_VEHICLE, this.allTowVehicleHandler, this );
                
                
		
		homePage.addEventListener( HomePage.VEHICLE_LIST_VIEW, this.getVehicleList, this );
		homePage.addEventListener( HomePage.VEHICLE_MAP_VIEW, this.getMapList, this );
		homePage.addEventListener( HomePage.LOCKED_VEHICLE, this.getLockedVehicleList, this );
		homePage.addEventListener( HomePage.VEHICLE_REFRESH, this.refreshVehicleList, this );
		
		homePage.addEventListener( HomePage.SORT_VID, this.vIdSort_clickHandler, this );
		homePage.addEventListener( HomePage.SORT_YEAR, this.vYearSort_clickHandler, this );
		homePage.addEventListener( HomePage.SORT_MAKE, this.vMakeSort_clickHandler, this );
		homePage.addEventListener( HomePage.SORT_MODEL, this.vModelSort_clickHandler, this );
		homePage.addEventListener( HomePage.SORT_SELER, this.vSellerSort_clickHandler, this );
		homePage.addEventListener( HomePage.SORT_CITY_STATE, this.vCityStateSort_clickHandler, this );
		homePage.addEventListener( HomePage.SORT_UPDATE_DATE, this.vUpdatedSort_clickHandler, this );
		homePage.addEventListener( HomePage.SORT_PRICE, this.vPriceSort_clickHandler, this );
		homePage.addEventListener( HomePage.SORT_ASSIST_BY, this.vAssistSort_clickHandler, this );
		homePage.addEventListener( HomePage.SORT_ACTION, this.vActionSort_clickHandler, this );
		homePage.addEventListener( HomePage.SORT_WATCHLIST, this.vWatchListSort_clickHandler, this );
		homePage.addEventListener( HomePage.SORT_MESSAGE, this.vMessageSort_clickHandler, this );
		homePage.addEventListener( HomePage.SEARCH, this.search_clickHandler, this );
                
	},
	
    get: function()
	{ 
		return this.viewComponent;
	},
	/*******/
	changeSortStateUnique: function(fieldName, divID)
	{
		var state = $(divID).attr("class");
		$(divID).removeClass('downState').addClass('sortNil');
		$(divID).removeClass('upState').addClass('sortNil');
		if(state == 'upState'){
			$(divID).removeClass('sortNil').addClass('downState');
		   	this.VProxy.setSortSettings(fieldName, 0);
		}else if (state == 'downState'){
			$(divID).removeClass('sortNil').addClass('upState');
			this.VProxy.setSortSettings(fieldName, 1);
		}else{
			$(divID).removeClass('sortNil').addClass('upState');
		   	this.VProxy.setSortSettings(fieldName, 1);
		}
	},
	search_clickHandler: function()
	{ 
		var searchString = document.getElementById("s").value;
		var criteria = document.getElementById("c").value;
		this.VProxy.searchWiseVehicleList(searchString, criteria);	
	},
	vIdSort_clickHandler: function()
	{ 
		this.changeSortStateUnique('vehicle_id', "#v-id-sort");
		
	},
	vYearSort_clickHandler: function()
	{ 
		this.changeSortStateUnique('year', "#v-year-sort");
	},
	vMakeSort_clickHandler: function()
	{ 
	    this.changeSortStateUnique('make', "#v-make-sort");
	},
	vModelSort_clickHandler: function()
	{ 
	   	this.changeSortStateUnique('model', "#v-model-sort");
	},
	vSellerSort_clickHandler: function()
	{ 
	  	this.changeSortStateUnique('first_name', "#v-seller-sort");
	},
	vCityStateSort_clickHandler: function()
	{ 
	  	this.changeSortStateUnique('state', "#v-city-state-sort");
	},
	vUpdatedSort_clickHandler: function()
	{ 
	   	this.changeSortStateUnique('update_date', "#v-updated-sort");
	},
	vPriceSort_clickHandler: function()
	{ 
	   	this.changeSortStateUnique('asking_price', "#v-price-sort");
	},
	vAssistSort_clickHandler: function()
	{ 
	   	this.changeSortStateUnique('assist_by', "#v-assist-sort");
	},
	vActionSort_clickHandler: function()
	{ 
	   	this.changeSortStateUnique('current_action', "#v-action-sort");
	},
	vWatchListSort_clickHandler: function()
	{ 
	  	this.changeSortStateUnique('is_watch', "#v-icon-sort");
	},
	vMessageSort_clickHandler: function()
	{ 
	  	this.changeSortStateUnique('message', "#v-icon-sort");
	},
	/*******/
	getVehicleList: function()
	{
		this.VProxy.getVehicleByPage(1);
	},
	refreshVehicleList: function()
	{
		$("#c").val('all');
		document.getElementById("s").value = '';
		this.VProxy.refreshVehicle();
	},
	getLockedVehicleList: function()
	{
		this.VProxy.getLockedVehicles();
	},
	getMapList: function()
	{
		//this.sendNotification( NotificationNames.SHOW_MAP_VIEW, 'map' );
		var bnd = new google.maps.LatLngBounds();
						for(var k=0;k<pointArray.length;k++){
							bnd.extend(pointArray[k]);
							
							
						}
						//listmap.setCenter( bnd.getCenter() );
                                                listmap.fitBounds(bnd);
			//listmap.setCenter( pointArray[1] );	
	},
        
        newTowVehicleHandler: function()
        { 
            if(current_page==AppConstants.VEHICLE_LIST)
            { 
               this.setVehicle_List();
               $('li').removeClass('active').addClass('inactive');
               $('#ta-new-vehicle-component').removeClass('inactive').addClass('active');  
		this.VProxy.getDefaultStage();
		$("#c").val('all');
		document.getElementById("s").value = '';
		currentStatus = 1;
                 
		this.VProxy.getVehicleByPage(1);
            }
            else
            {
                 window.location='vehicle_list.php?tab=1';
            }    
        },
        inprogressTowVehicleHandler: function()
        {
           if(current_page==AppConstants.VEHICLE_LIST)
            {
               this.setVehicle_List();
               $('li').removeClass('active').addClass('inactive');
               $('#ta-progress-vehicle-component').removeClass('inactive').addClass('active');  
               
		this.VProxy.getDefaultStage();
		$("#c").val('all');
		document.getElementById("s").value = '';
		currentStatus = 2;
                 
		this.VProxy.getVehicleByPage(1);
                
             
            }
            else
            {
                 window.location='vehicle_list.php?tab=2';
            }    
        },
        deliveredTowVehicleHandler: function()
        {
           if(current_page==AppConstants.VEHICLE_LIST)
            { 
               this.setVehicle_List();
               $('li').removeClass('active').addClass('inactive');
               $('#ta-delivered-vehicle-component').removeClass('inactive').addClass('active');  
		this.VProxy.getDefaultStage();
		$("#c").val('all');
		document.getElementById("s").value = '';
		currentStatus = 3;
                 
		this.VProxy.getVehicleByPage(1);
            }
            else
            {
                  window.location='vehicle_list.php?tab=3';
            }    
        },
        allTowVehicleHandler: function()
        {
           if(current_page==AppConstants.VEHICLE_LIST)
            { 
               this.setVehicle_List();
               $('li').removeClass('active').addClass('inactive');
               $('#ta-all-vehicle-component').removeClass('inactive').addClass('active');  
		this.VProxy.getDefaultStage();
		$("#c").val('all');
		document.getElementById("s").value = '';
		currentStatus ="All";
                 
		this.VProxy.getVehicleByPage(1);
            }
            else
            {
                  window.location='vehicle_list.php?tab=4';
            }    
        },
        
        
	addNewVehicleHandler: function()
	{
		this.VProxy.setMode( AppConstants.APP_ADD);
		
		this.sendNotification( NotificationNames.ADDNEW, 'fcb' );
	},
        
         setVehicle_List: function()
        {
               $("#vehicle-list-page").show();
               $("#vehicle-list-area").show();
               $("#vehicle-list-component").show();
               $("#page-map").hide();
               $("#vehicle-list_button").button('toggle');
        },
	
	newSubmissionHandler: function()
	{	
             if(current_page==AppConstants.VEHICLE_LIST)
            { 
               this.setVehicle_List();
               $('li').removeClass('active').addClass('inactive');
               $('#new-vehicle-component').removeClass('inactive').addClass('active');  
		this.VProxy.getDefaultStage();
		$("#c").val('all');
		document.getElementById("s").value = '';
		currentStatus = 'New Submission';
                 
		this.VProxy.getVehicleByPage(1);
            }
            else
            {
                 window.location='vehicle_list.php?tab=1';
            }    
              
	},
	
	bisAcceptedHandler: function()
	{  
          if(current_page==AppConstants.VEHICLE_LIST)
          {
               this.setVehicle_List();   
               $('li').removeClass('active').addClass('inactive');
               $('#bidaccepted-vehicle-component').removeClass('inactive').addClass('active');     
		this.VProxy.getDefaultStage();
		$("#c").val('all');
		document.getElementById("s").value = '';
		currentStatus = 'Bid Accepted';
		this.VProxy.getVehicleByPage(1);
          }
          else
           {
               window.location='vehicle_list.php?tab=2';
           }   
	},
	
	inPossessionHandler: function()
	{
            if(current_page==AppConstants.VEHICLE_LIST)
            { 
                this.setVehicle_List();    
                $('li').removeClass('active').addClass('inactive');
                $('#inpossesion-vehicle-component').removeClass('inactive').addClass('active');  
		this.VProxy.getDefaultStage();
		$("#c").val('all');
		document.getElementById("s").value = '';
		currentStatus = 'Now In Possession';
		this.VProxy.getVehicleByPage(1);
             }
           else
           {
              window.location='vehicle_list.php?tab=3';     
           }    
	},
	
	allVehicleHandler: function()
	{ 
            if(current_page==AppConstants.VEHICLE_LIST)
            {  
                this.setVehicle_List();    
               $('li').removeClass('active').addClass('inactive');
               $('#all-vehicle-component').removeClass('inactive').addClass('active');
		this.VProxy.getDefaultStage();
		$("#c").val('all');
		document.getElementById("s").value = '';
                
		currentStatus = 'All';
		this.VProxy.getVehicleByPage(1);
             }
           else
           {
              window.location='vehicle_list.php?tab=4';     
           }   
              
               
	},
	listNotificationInterests: function()
	{
		return [		
				
				
		];
	},
	handleNotification: function( note )
	{ 
		
		switch ( note.getName() )
		{  
			
			  
			
		}
	},
	unlockVehicle: function(vehiclID)
	{
		this.VProxy.unlockVehicle(vehiclID);
	},
	showLockedVehicle: function()
	{
		var vehicle = this.VProxy.getLockedTable();
		document.getElementById("locked-list").innerHTML="";
		for(i=0;i<vehicle.lockedvehicles.length;i++)
		{ 
			var innerHtmlCode = '';
			
			if(vehicle.lockedvehicles[i].is_read == true){ 
				 innerHtmlCode += "<div id='vehicle-list' class='listBold'>";
			}else{
				innerHtmlCode += "<div id='vehicle-list'>";
			}
			innerHtmlCode +="<div class='v-image'><img src='" + vehicle.lockedvehicles[i].imageurl + "' width='50' height='40'/></div><div class='v-id'>" + vehicle.lockedvehicles[i].vehicle_id + "</div><div class='v-year'>" + vehicle.lockedvehicles[i].year + "</div><div class='v-make'>" +vehicle.lockedvehicles[i].make + "</div><div class='v-model'>" + vehicle.lockedvehicles[i].model + "</div><div class='v-seller'>" + vehicle.lockedvehicles[i].first_name + "</div><div class='v-city-state'>" + vehicle.lockedvehicles[i].v_city + " / " +vehicle.lockedvehicles[i].v_state + "</div><div class='v-updated'>" + vehicle.lockedvehicles[i].update_date + "</div><div class='v-price'>$" + vehicle.lockedvehicles[i].asking_price + "</div><div class='v-assist'>" + vehicle.lockedvehicles[i].assist_by + "</div><div class='v-action'>" + vehicle.lockedvehicles[i].current_action + "</div><div class='v-icons' onClick='thishome.unlockVehicle("+ vehicle.lockedvehicles[i].vehicle_id +")'><a href='#'>Unlock</a>";			
			
			if(vehicle.lockedvehicles[i].isReminder == true){ 
				 innerHtmlCode += "<img src='" + vehicle.lockedvehicles[i].imageurl + "' width='24' height='24'/>";
			}else if(vehicle.lockedvehicles[i].is_watch == true){
				 innerHtmlCode += "<img src='" + vehicle.lockedvehicles[i].imageurl + "' width='24' height='24'/>";
			}
			
			innerHtmlCode += "</div></div>";
			document.getElementById("locked-list").innerHTML += innerHtmlCode; 
		}
	}
	
	

});

